package com.sdk.markting.util;


import com.android.sdk.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.widget.Toast;

public class RatingConfig {

	private static final String RATE_PRE_FILE = "rate_app_file";

	private static RatingConfig sInstance;

	public static void init(Context appContext){
		if(sInstance == null){
			sInstance = new RatingConfig(appContext.getApplicationContext());
		}
	}

	public static RatingConfig getInstance(){
		if(sInstance == null){
			throw new RuntimeException("please first call init(c)");
		}
		return sInstance;
	}

	private RatingConfig(Context c){
		mContext = c;
		mPreferences = mContext.getSharedPreferences(RATE_PRE_FILE, Context.MODE_PRIVATE);
	}

	private Context mContext;
	private SharedPreferences mPreferences;


	private static final String KEY_RATE_APP = "rate_app";
	private static final String KEY_LAST_LAUNCH_TIME = "last_launch_time";

	// 是否已经打过分了
	public boolean isRate(){
		return mPreferences.getBoolean(KEY_RATE_APP, false);
	}

	// 最小时间间隔是1d,一天最多弹出一次
	private static final long MIN_TIME_INTERVAL = 24 * 60 * 60 * 1000;

	public boolean isShowByTime() {
		long lastLaunchTime = mPreferences.getLong(KEY_LAST_LAUNCH_TIME, 0);
		if(System.currentTimeMillis() - lastLaunchTime > MIN_TIME_INTERVAL){
			return true;
		}
		return false;
	}

	public void saveLastLaunchTime(long time){
		mPreferences.edit().putLong(KEY_LAST_LAUNCH_TIME, time).commit();
	}

	public void saveIsRate(boolean isRate){
		mPreferences.edit().putBoolean(KEY_RATE_APP, isRate).commit();
	}

	// 首次启动大于4次后，才开始弹出rate dialog
	private static final int MIN_LAUNCH_COUNT = 4;
	private static final String KEY_LAUNCH_COUNT = "launch_count";

	// 启动次数要大于4次才弹出ratedialog
	public boolean isShowByLaunchCount() {
		long launchCount = mPreferences.getLong(KEY_LAUNCH_COUNT, 0);
		if(launchCount >= MIN_LAUNCH_COUNT){
			return true;
		} else {
			mPreferences.edit().putLong(KEY_LAUNCH_COUNT, launchCount + 1).commit();
			return false;
		}
	}

	public static void checkShowRateDiaog(Activity a){
	    RatingConfig.init(a.getApplicationContext());
	    RatingConfig config = RatingConfig.getInstance();
		if(!config.isRate() && config.isShowByTime() && config.isShowByLaunchCount()){
			config.saveLastLaunchTime(System.currentTimeMillis());
			showRateDialog(a);
		}
	}

	private static void showRateDialog(final Activity a){
		new AlertDialog.Builder(a)
		.setTitle(a.getText(R.string.rate_dialog_title))
		.setMessage(a.getText(R.string.rate_dialog_message))
		.setPositiveButton(a.getText(R.string.rate_dialog_btn_ok), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			    RatingConfig.getInstance().saveIsRate(true);
				doRating(a, a.getPackageName());
			}
		})
		.setNegativeButton(a.getText(R.string.rate_dialog_btn_cancel), null)
		.create()
		.show();
	}

	private static void doRating(Context context, String packageName) {
		Uri marketUri = Uri.parse("market://details?id=" + packageName);
		Intent intent = new Intent(Intent.ACTION_VIEW, marketUri);
		intent.addCategory(Intent.CATEGORY_BROWSABLE);
		try {
			context.startActivity(intent);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(context, context.getText(R.string.rate_no_market), Toast.LENGTH_SHORT).show();
		}
	}
}

